var bankCities =[]; //市
var bankProvinces=[]; //省
var mtBankCategories=[]; //满期银行
var nbPaymentMethods=[]; //支付方式
var pmBankCategories=[]; //银行
var investTimes =[]; //投资选项
var app={}; //数据
var appBase={};
$(function(){	
	//初始化视图
	initView();
	//绑定button触发事件
	bindFun();
});
function bindFun(){	
// 点击事件
	
	
	$("#before").click(function(){
		window.location.href="/pad_marketing/benefit/index.action";
	});
	
	$("#next").click(function(){
		if($("#insurancePaymentAcc").attr("style")!="display: none;"){
			var chArr = inputCheck("otherForm");
			if(chArr[0])
			{
				if("交费银行账户号码不能为空！"==chArr[1]){
					alertMsg("请正确填写交费银行账户号码");
					return;
				}
				alertMsg(chArr[1]);
				return;
			}
		}	
		var nbPremium =$("#nbPremium").html();
		var nbPaymentMethods = $("#nbPaymentMethods").val();
		if(appBase.nbPremium>=20000){
			if(nbPaymentMethods=="02"){
				alertMsg("保费超过20000时,不能选择在线支付方式");
				return;
			}
		}
		var mathT = $("input[name='mathTo']");
		if(mathT.length!=0){
			var num=0;
			$("input[name='mathTo']").each(function(index,me,arg){
				num+=parseInt($(me).val()==""?0:$(me).val());
			});
			if(num!=100)
			{
				alertMsg("投资账户分配比例合计应该为100%");
				return;
			}
		}
		
		if(mtCheck()){
			alertMsg("请您完整并正确填写满期银行账户信息");
			return;
		}
		
		if(appBase.productGroup3 =="UNIT LINK" || appBase.productGroup3 =="UNIVERSAL"){
			var invOption = $("#invOption").val();
			if(invOption){
				
			}else{
				alertMsg("请你勾选年金、投资产品投资选项");
				return;
			}
		}
		
		saveData();
	});
}
function initView(){
	getData();
	initBankProvinces();
	initBankCities();
	initMtBankCategories();
	initNbPaymentMethods();
	initPmBankCategories();
	initInvestTimes();
	initData();
	
}

function getData(){
	var appbase = sessionStorage.getItem("appBase");
	if(appBase==null||appBase==""){
		alertMsg("session已经过期");
		return;
	}
	var appB = eval('(' + appbase + ')');
	var params = {
		"appId" : appB.appId,
		"orderId" : appB.orderId,
		"sign" : appB.sign
	};	
	padMarketing.ajaxNormal('/pad_marketing/main/appeditothers.action', params,
			function(result) {
				
				 investTimes = result.investTimes;
				 bankCities =result.bankCities;
				 bankProvinces=result.bankProvinces;
				 mtBankCategories=result.mtBankCategories;
				 nbPaymentMethods=result.nbPaymentMethods;
				 pmBankCategories=result.pmBankCategories;
				 app =result.app;
				 appBase=result.app.appBase;
				 console.log(result)
			}, function(data) {
				alertMsg('失败',1);
			},false);
}
//省
function initBankProvinces(){
	var html="";
	for(var i in bankProvinces){
		html +='<option value='+bankProvinces[i].val+'>'+bankProvinces[i].text+'</option>';
	}
	$("#pmBaProvince").append(html);
	$("#mtBaProvince").append(html);	
}
//市
function initBankCities(str){
	if(str=="pm"){
		var html="";
		html+='<option value="">请选择</option>';
		for(var i in bankCities){
			if($("#pmBaProvince").val()==bankCities[i].province){
				html +='<option value='+bankCities[i].areaCode+'>'+bankCities[i].city+'</option>';
			}		
		}
		$("#pmBaAreaCode").html(html);	
		
	}else if(str=="mt"){
		var htm="";
		htm+='<option value="">请选择</option>';
		for(var i in bankCities){
			if($("#mtBaProvince").val()==bankCities[i].province){
				htm +='<option value='+bankCities[i].areaCode+'>'+bankCities[i].city+'</option>';
			}		
		}
		$("#mtBaAreaCode").html(htm);
		
	}else if(str=="all"){
		var html="";
		html+='<option value="">请选择</option>';
		for(var i in bankCities){
			if($("#pmBaProvince").val()==bankCities[i].province){
				html +='<option value='+bankCities[i].areaCode+'>'+bankCities[i].city+'</option>';
			}		
		}
		$("#pmBaAreaCode").html(html);	
		var htm="";
		htm+='<option value="">请选择</option>';
		for(var i in bankCities){
			if($("#mtBaProvince").val()==bankCities[i].province){
				htm +='<option value='+bankCities[i].areaCode+'>'+bankCities[i].city+'</option>';
			}		
		}
		$("#mtBaAreaCode").html(htm);
	}
	
}
function initMtBankCategories(){
	var html="";
	for(var i in mtBankCategories){
		html +='<option value='+mtBankCategories[i].val+'>'+mtBankCategories[i].text+'</option>';
	}
	$("#mtBaBankCategoryId").append(html);

}
function initInvestTimes(){
	var html="";
	for(var i in investTimes){
		html +='<option value='+investTimes[i].val+'>'+investTimes[i].text+'</option>';
	}
	$("#invOption").append(html);
}
function initPmBankCategories(){
	var html="";
	for(var i in pmBankCategories){
		html +='<option value='+pmBankCategories[i].val+'>'+pmBankCategories[i].text+'</option>';
	}
	$("#pmBaBankCategoryId").append(html);
}
function initNbPaymentMethods(){
	var html="";
	for(var i in nbPaymentMethods){
		if(nbPaymentMethods[i].code!="10"){
			html +='<option value='+nbPaymentMethods[i].code+'>'+HtmlEncode(nbPaymentMethods[i].name)+'</option>';
		}
	}
	$("#nbPaymentMethods").data("app",appBase);
	$("#nbPaymentMethods").append(html);
	$("#nbPaymentMethods").val("01");
}
function initData(){
//获取数据
	var invAccounts = app.invAccounts;
	var appBase = app.appBase;
//一年期是否自动申请自动续保
	if(appBase.productGroup5=="ONE YEAR TERM"){
		if(appBase.autoRenew=="Y"){
			$("#yearYes").attr("class","radio_yes");
			$("#yearNo").attr("class","radio_no");
		}else{
			$("#yearNo").attr("class","radio_yes");
			$("#yearYes").attr("class","radio_no");
		}
		
	}else{
		$("#oneYearMain").hide();
	}
	
	
//年金累计生息
	if(appBase.annuityCaEditable){
		//累计生息
		if(appBase.annuityCa)
		{
			$("#yearY").attr("class","check_yes");
		}else{
			$("#yearY").attr("class","check_no");
		}
		
	}else{
		$("#yearAdd").hide();
		$("#yearAdd").prev().hide();
	}
	
//缴费账户动态显示 
	var flag = needRenewPayment(appBase);
	if(!flag){
		$("#renewalPayMethodp").hide();
	}
	flag = needPayment(appBase);
	if(!flag){
		$("#insurancePaymentAcc").hide();
		$("#insurancePaymentAcc").prev().hide();
	}
//满期银行动态显示
	if(appBase.productGroup4 =="ANNUITY" || appBase.productGroup4 =="ENDOWMENT" ){
	
		for(var i in app.cps){
			if(app.cps[i].rlToMins!="SLF" &&app.cps[i].cpNo==1){
				$("#sameMoney").hide();
			}
		}
	}else{
		$("#mtDiv").hide();
		$("#mtDiv").prev().hide();
	}
//投连产品动态显示
	if(appBase.productGroup3 =="UNIT LINK" || appBase.productGroup3 =="UNIVERSAL"){
		var html ="";
		for(var i in invAccounts){
			html+='<div data-invAccName="'+JSON.stringify(invAccounts[i])+'" class="col-md-3">';
			html+='<p class="lh-45 text-center">'+HtmlEncode(invAccounts[i].invAccName)+'</p>';
			html+='<div class="input-group">';
			html+='<input name="mathTo" value="'+invAccounts[i].invRate+'"type="number" min="1" max="100" oninput="changeNum()" class="form-control"/>';
			html+='<label class="input-group-addon">%</label>';
			html+='</div>'; 
			html+='<div class="blank-10"></div>';
			html+='</div>';
		}
		$("#cpData").html(html);
		if(appBase.autoRebalanceEditable ){
			if(appBase.autoRebalance)
			{
				$("#balanceY").attr("class","check_yes");
			}else
			{
				$("#balanceY").attr("class","check_no");
			}
			
		}else{
			$("#balanceDiv").hide();
		}
	}else{
		$("#tzcp").hide();
		$("#tzcpl").hide();
	}
	if(appBase.allowAnnuityOption)
	{
		//年金选择权
		if(appBase.annuityOption)
		{
			$("#annuityOptions").attr("class","gray check_yes");
		}else{
			$("#annuityOptions").attr("class","gray check_no");
		}
	}else
	{
		$("#annuityOptionsDiv").hide();
		$("#annuityOptionsDiv").prev().hide();
	}

//初始化apl
	if(appBase.aplEditable){
		if(appBase.apl)
		{
			$("#matChoice").attr("class","check_yes");
		}else
		{
			$("#matChoice").attr("class","check_no");
		}
	}else{
		$("#aplEditable").hide();
		$("#aplEditable").prev().hide();
	}
	
//具体信息加载
	var cps = app.cps;
	$("#nbPremium").html(formatMoney(appBase.nbPremium));
	$('#otherForm').formEdit(appBase);
	changeAddress("all");
	$("#pmBaAreaCode").val(appBase.pmBaAreaCode);	
	$("#mtBaAreaCode").val(appBase.mtBaAreaCode);
	changeMethod();
	
	for(var i in cps){
		if(cps[i].cpNo==1){
			$("#authorizedHolder").html(HtmlEncode(cps[i].name));
		}else if(cps[i].cpNo==3){
			$("#MtauthorizedHolder").html(HtmlEncode(cps[i].name));
		}
	}
	if(myScroll)
		myScroll.refresh();
}
function changeAddress(str){
	initBankCities(str);
}
function changeMethod(){
	var code =$("#nbPaymentMethods").val();
	
	var nbPremium = $("#nbPremium").html();
	
	if(appBase.nbPremium>=20000){
		if(code=="02")
		{
			alertMsg("在线支付的限额不能超过20000元",0,function(){
				$("#nbPaymentMethods").val("");
			});		
			return;
		}
	}

	
	if(code!="01"){
		/*$("#bankPayment").hide();*/
		var obj = $("#nbPaymentMethods").data("app");
		obj.nbPaymentMethod=code;
		var cla= $("#yearYes").attr("class");
		obj.autoRenew=cla=="radio_yes"?"Y":"N";
		if(!needPayment(obj)){
			$("#insurancePaymentAcc").hide();
			$("#insurancePaymentAcc").prev().hide();
		}
		
		
	}else{
		/*$("#bankPayment").show();*/
		$("#insurancePaymentAcc").show();
		$("#insurancePaymentAcc").prev().show();
	}
	if(myScroll)
		myScroll.refresh();
}
function saveData(){
	var appBase = app.appBase;
	var obj =$("#otherForm").serializeObject();
	for(var i in obj.mathTo){
		app.invAccounts[i].invRate=obj.mathTo[i];
	}
	delete obj.mathTo;
	if(appBase.allowAnnuityOption){
		var cla=$("#annuityOptions").attr("class");
		if(cla=="gray check_yes")
		{
			obj.annuityOption = true;
		}else{
			obj.annuityOption = false;
		}
	}
	if(appBase.autoRebalanceEditable){
		var cl=$("#balanceY").attr("class");
		if(cl=="check_yes")
		{
			obj.autoRebalance = true;
		}else{
			obj.autoRebalance = false;
		}
	}
	if(appBase.productGroup5=="ONE YEAR TERM"){
		var cl=$("#yearYes").attr("class");
		if(cl=="radio_yes")
		{
			obj.autoRenew = "Y";
		}else{
			obj.autoRenew = "N";
		}
	}
	if(appBase.aplEditable){
		var c = $("#matChoice").attr("class");
		if(c=="check_yes"){
			obj.apl = true;
		}else{
			obj.apl = false;
		}
	}
	
	if(appBase.annuityCaEditable){
		var y =$("#yearY").attr("class");
		if(y=="check_yes"){
			obj.annuityCa = true;
		}else{
			obj.annuityCa = false;
		}
	}
	
	
	
	var param=$.extend( true,  {},  
			appBase ,  obj ); 
	
	app.appBase=param;
	
	padMarketing.ajaxNormal(
			"readyOther.action",
			app,
			function (data){
				if(app.appBase.mdpPlanEditable){
					sessionStorage.setItem("appBase",JSON.stringify(app.appBase));
					window.location.href="/pad_marketing/futures/futuresPlan.action";
					return;
				}
				window.location.href="gotoHealthContent.action";				
			},
			function(data){}
	);
	
}
function changeNum(){
	var num=0;
	$("input[name='mathTo']").each(function(index,me,arg){
		num+=parseInt($(me).val()==""?"0":$(me).val());
	});
	$("#toatlData").html(num);
}
function sameMoney(){	
	$("#mtBaBankCategoryId").val($("#pmBaBankCategoryId").val());
	$("#mtBaProvince").val($("#pmBaProvince").val());
	changeAddress("mt");
	$("#mtBaAreaCode").val($("#pmBaAreaCode").val());
	$("#mtBaNo").val($("#pmBaNo").val());
}
function mtCheck(){
	var flag = false;
	var mtBaBankCategoryId = $("#mtBaBankCategoryId").val();
	var mtBaProvince = $("#mtBaProvince").val();
	var mtBaAreaCode = $("#mtBaAreaCode").val();
	var mtBaNo = $("#mtBaNo").val();
	if(mtBaBankCategoryId || mtBaProvince || mtBaAreaCode || mtBaNo){
		if(mtBaBankCategoryId && mtBaProvince && mtBaAreaCode && mtBaNo){
		
		}else{			
			flag = true;
			return flag;
		}
	}
	return flag;
}
function oneYearCharge(index){
	var flag=true;
	var flag2=true;
	//var cla = $("#oneYear").attr("class");
	//var cla = $("#yearYes").attr("class");
	if(index==1){
		appBase.autoRenew="Y";
		appBase.nbPaymentMethod=$("#nbPaymentMethods").val();
		flag=needRenewPayment(appBase);
		flag2=needPayment(appBase);
	}else{
		appBase.autoRenew="N";
		appBase.nbPaymentMethod=$("#nbPaymentMethods").val();
		flag=needRenewPayment(appBase);
		flag2=needPayment(appBase);
	}
	
	
	
	if(flag){
		$("#renewalPayMethodp").show();
	}else{
		$("#renewalPayMethodp").hide();
	}
	
	if(flag2){
		$("#insurancePaymentAcc").show();
		$("#insurancePaymentAcc").prev().show();
	}else{
		$("#insurancePaymentAcc").hide();
		$("#insurancePaymentAcc").prev().hide();
	}
}
